<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataQbxx } from "@/api/search";
import { addOrUpdateQbxx } from "@/api/addOrUpdate";

const city = window.globalObj.name;
const editorRef = shallowRef();

const toolbarConfig = {};
const editorConfig = {
  placeholder: "请输入内容...",
};
const mode = ref("default");

// 加载html
const initHtml = () => {
  searchDataQbxx({
    m: "信息防护引言",
  }).then((res) => {
    const { data } = res;
    valueHtml.value = data.find((x) => x.k === "信息防护引言")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

const valueHtml = ref(
  "主要明确数据、网络、平台防护的基本手段、安全策略、容灾备份等事项。"
);

const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateQbxx({
      m: "信息防护引言",
      data: [
        {
          k: "信息防护引言",
          v: valueHtml.value,
        },
      ],
    })
      .then((result) => {
        if (result.code === 200) {
          ElMessage.success("操作成功");
          initHtml();
        } else {
          ElMessage.error(result.msg);
        }
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (!editor) return;
  editor.destroy();
});
</script>
<template>
  <div class="">
    <div class="top-15">
      主要明确指挥所指控系统的设备调配、软件部署、数据加载、业务引接，指挥所对外通联的主用、备用、保底手段等。
    </div>
    <FirstTitle name="引言" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>